﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Avalonia.BiliBili.Libs.Models
{
    public sealed class ListArray<TKey, TValue, TDesc>
    {
        private List<Tuple<TKey, TValue, TDesc>> Tuples { get; }
        public ListArray()
        {
            Tuples = new List<Tuple<TKey, TValue, TDesc>>();
        }
        public void Add(TKey key, TValue value, TDesc desc)
        {
            var tuple = new Tuple<TKey, TValue, TDesc>(key, value, desc);
            Tuples.Add(tuple);
        }
        public void UpdateData(TKey key, TValue value)
        {
            try
            {
                var item = Tuples.FirstOrDefault(x => x.Item1.Equals(key));
                var index = Tuples.IndexOf(item);
                var tuple = Tuple.Create(item.Item1, value, item.Item3);
                Tuples.Remove(item);
                Tuples.Insert(index, tuple);
            }
            catch { }
        }
        public void UpdateData(TKey key, TDesc desc)
        {
            try
            {
                var item = Tuples.FirstOrDefault(x => x.Item1.Equals(key));
                var index = Tuples.IndexOf(item);
                var tuple = Tuple.Create(item.Item1, item.Item2, desc);
                Tuples.Remove(item);
                Tuples.Insert(index, tuple);
            }
            catch { }
        }
        public Tuple<TKey, TValue, TDesc> First(Func<Tuple<TKey, TValue, TDesc>, bool> func = null)
        {
            if (func != null)
                return Tuples.FirstOrDefault(func);
            else
                return Tuples.FirstOrDefault();
        }
        public void UpdateData(TKey key, TValue value, TDesc desc)
        {
            try
            {
                var item = Tuples.FirstOrDefault(x => x.Item1.Equals(key));
                var index = Tuples.IndexOf(item);
                var tuple = Tuple.Create(item.Item1, value, desc);
                Tuples.Remove(item);
                Tuples.Insert(index, tuple);
            }
            catch { }
        }
        public void Clear()
        {
            Tuples.Clear();
        }
        public List<Tuple<TKey, TValue, TDesc>>.Enumerator GetEnumerator()
        {
            return Tuples.GetEnumerator();
        }
    }
}